$('document').ready(function () {
    // 加载页面时触发的事件
    
    // name-input框事件
    $('#name-input').blur(function () {
        if ($('#name-input').val() == '') {
            $('#name-input').css('border','2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        } else if ($('#name-input').val().length > 12) {
            $('#name-input').prop('placeholder', '长度不能大于12位!')
            $('#name-input').val('')
            $('#name-input').css('border','2px solid red')
        } else {
            $('#name-input').css('border','2px solid green')
        }
    })
    $('#name-input').keyup(function () {
        if (($('#name-input').val() !== '')) {
            $('#name-input').css('border','2px solid green')
        }else { 
            $('#name-input').css('border','2px solid red')
            $('#name-input').prop('placeholder', '姓名不能为空!')
        }

    })
    // password-input框事件
    $('#password-input').blur(function () {
        if ($('#password-input').val() == '') {
            $('#password-input').css({
                'border': '2px solid red',
            })
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#checkpsw').css('cursor','auto')
        } else if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#password-input').css('border','2px solid green')
            $('#checkpsw').css('cursor','pointer')
        }
    })
    $('#password-input').keyup(function () {
        if ($('#password-input').val().length < 9 && $('#password-input').val().length > 0) {
            $('#checkpsw').css('cursor','pointer')
            $('#password-input').css('border','2px solid green')
        }else {
            $('#checkpsw').css('cursor','auto')
            $('#password-input').prop('placeholder', '密码不能为空!')
            $('#password-input').css('border','2px solid red')
        }

    })
    // 查看密码
    $('#checkpsw').click(function(){
        if ($('#password-input').attr('type')=='password') {
            $('#password-input').attr('type','text')
        }else if($('#password-input').attr('type')=='text'){
            $('#password-input').attr('type','password')
        }
    })

    // email-input框事件
    var emailzz = new RegExp("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$");
    $('#email-input').blur(function () {
        if ($('#email-input').val() == '') {
            $('#email-input').css({
                'border': '2px solid red',
            })
            $('#email-input').prop('placeholder', '邮箱不能为空!')
        } else if (!emailzz.test($('#email-input').val())) {
            $('#email-input').prop('placeholder', '邮箱格式错误!')
            $('#email-input').val('')
        } else {
            $('#email-input').css('border','2px solid green')
        }
    })
    $('#email-input').keyup(function () {
        if (emailzz.test($('#email-input').val())) {
            $('#email-input').css('border','2px solid green')
        } else if (!emailzz.test($('#email-input').val())) {
            $('#email-input').prop('placeholder', '邮箱格式错误!')
            $('#email-input').css('border','2px solid red')
        }
    })
    // company-input框事件
    $('#company-input').blur(function () {
        if ($('#company-input').val() == '') {
            $('#company-input').css({
                'border': '2px solid red',
            })
            $('#company-input').prop('placeholder', '工作单位不能为空!')
        } else if ($('#company-input').val().length > 0) {
            $('#company-input').css('border','2px solid green')
        }
    })
    $('#company-input').keyup(function () {
        if ($('#company-input').val().length > 0) {
            $('#company-input').css('border','2px solid green')
        }else {
            $('#company-input').prop('placeholder', '工作单位不能为空!')
            $('#company-input').css('border','2px solid red')
        }

    })

    // 获取验证码按钮
    var codetime=60;
    function cd(){
        if(codetime==0){
            $('#code-button').text('获取验证码')
            codetime=60;
        }else{
            $('#code-button').text(codetime + "秒后重新发送")
            codetime--
            setTimeout(function(){cd()},1000)
        }
    }
    $('#code-button').click(function(){
        if(emailzz.test($('#email-input').val())){
            if($(this).text()=="获取验证码"){
                $.ajax({
                    type:'GET',
                    url:'http://111.229.120.67/gen/register-code',
                    data:{
                        "email":$('#email-input').val()
                    },
                    xhrFields:{
                        withCredentials:true
                    },
                    success:function(data){
                        if(data.statusCode==0){
                            $('#myModal-public').modal({})
                            $('#myModal-public h4').html('已发送')
                            $('#myModal-public-body').html('验证码已成功发送')
                        }else{
                            $('#myModal-public').modal({})
                            $('#myModal-public h4').html('请求超时')
                            $('#myModal-public-body').html('请求超时，请重试')
                        }
                    }
                })
                cd();
            }else{
                $('#myModal-public').modal({})
                $('#myModal-public h4').html('请勿频繁点击')
                $('#myModal-public-body').html('验证码获取失败')
            }
        }else{
            $('#myModal-public').modal({})
            $('#myModal-public h4').html('未填写邮箱！')
            $('#myModal-public-body').html('验证码获取失败')
        }
    })
    // -------------------------------------------------------------
    // 切换页面
    $('#login-tab').click(function(){
        window.location.href='login.html'
    })

    // 注册
    $('#register').click(function(){
        if(($('#name-input').val() !== '') && $('#password-input').val().length < 9 && $('#password-input').val().length > 0 && emailzz.test($('#email-input').val()) && $('#company-input').val().length>0 && $('#canvas-code').val()!==''){
            $.ajax({
                type:'POST',
                url:'http://111.229.120.67/gen/register',
                data:{
                    "realname":$('#name-input').val(),
                    "email":$('#email-input').val(),
                    "checkcode":$('#canvas-code').val(),
                    "password":$('#password-input').val(),
                    "unit":$('#company-input').val()
                },
                xhrFields:{
                    withCredentials:true
                },
                success:function(data){
                    if(data.statusCode==0){
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('注册成功')
                        $('#myModal-public-body').html('用户：'+$('#name-input').val()+'成功注册')
                    }else if(data.statusCode==-3){
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('注册失败')
                        $('#myModal-public-body').html('未获取验证码')
                    }else{
                        $('#myModal-public').modal({})
                        $('#myModal-public h4').html('注册失败')
                        $('#myModal-public-body').html('注册失败，请重试')
                    }
                }
            })
        }else if(!emailzz.test($('#email-input').val())){
            $('#email-input').prop('placeholder', '邮箱格式错误!')
            $('#email-input').css('border','2px solid red')
        }else if($('#password-input').val()=='' || $('#password-input').val().length>9){
            $('#password-input').prop('placeholder', '密码格式错误!')
            $('#password-input').css('border','2px solid red')
        }else if($('#company-input').val()==''){
            $('#company-input').prop('placeholder', '工作单位不能为空!')
            $('#company-input').css('border','2px solid red')
        }else if($('#name-input').val()==''){
            $('#name-input').prop('placeholder', '姓名不能为空!')
            $('#name-input').css('border','2px solid red')
        }else if($('#canvas-code').val()==''){
            $('#canvas-code').prop('placeholder', '验证码不能为空!')
            $('#canvas-code').css('border','2px solid red')
        }
    })
    // --------------------------
    // 模态框
    $('#myModal .modal-footer button').eq(1).click(function(){
        window.location.href="login.html"
    })
})